package com.douqu.game.core.entity;

import com.douqu.game.core.controller.PlayerController;
import com.douqu.game.core.e.E_DropType;
import com.douqu.game.core.entity.common.CommonData;
import com.douqu.game.core.entity.common.TwoTuple;
import com.douqu.game.core.entity.ext.BagInfo;
import com.douqu.game.core.entity.sprite.Player;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;
import sun.plugin.com.event.COMEventHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author: Gavin
 * Description: 掉落
 * Date: 2017-08-03 16:29-29
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */

public class DropGroup extends GameObject {

    /**
     * 对应E_DropType枚举里的值
     */
    public int propType; //道具掉落类型

    public int assetType; //资源掉落类型

    public int cardType; //兵种掉落类型

    public DropObject[] props; //道具掉落配置

    public DropObject[] assets; //资源掉落配置

    public DropObject[] cards; //卡片掉落配置


    /**
     * 领取奖励
     * @param playerController
     */
    public DropResult reward(PlayerController playerController)
    {
        BagInfo bagInfo = (BagInfo) playerController.getPlayer().getExtInfo(BagInfo.class);

        DropResult result = new DropResult();
        List<DropObject> cards = getCardList();
        TwoTuple<CommonData,CommonData> cardResult = null;
        Map<Integer, CommonData> cardPropMap = new ConcurrentHashMap<>();
        for(DropObject dropObject : cards)
        {
            cardResult = bagInfo.addCard(playerController, dropObject.id, dropObject.count);
            if(cardResult != null)
            {
                if(cardResult.getFirst() == null)
                {
                    if(cardResult.getSecond() != null)
                    {
                        cardPropMap.put(cardResult.getSecond().id, cardResult.getSecond());
                    }
                }
                else
                {
                    result.addCard(cardResult.getFirst());
                }
            }
        }
        //掉落的道具
        List<DropObject> props = getPropList();
        int tempCount = 0;
        for(DropObject dropObject : props)
        {
            if(cardPropMap.get(dropObject.id) != null)
            {
                tempCount = dropObject.count + cardPropMap.get(dropObject.id).value;
                cardPropMap.remove(dropObject.id);
            }
            else
            {
                tempCount = dropObject.count;
            }

            bagInfo.addGoods(playerController, dropObject.id, tempCount);

            result.addProp(new CommonData(dropObject.id, tempCount));
        }

        for(Integer id : cardPropMap.keySet())
        {
            bagInfo.addGoods(playerController, id, cardPropMap.get(id).value);

            result.addProp(new CommonData(id, cardPropMap.get(id).value));
        }

        //掉落的资源
        List<DropObject> assets = getAssetList();
        for(DropObject dropObject : assets)
        {
            bagInfo.addAsset(dropObject.id, dropObject.count);

            result.addAsset(new CommonData(dropObject.id, dropObject.count));
        }

        return result;
    }

    /**
     * 领取奖励
     * @param bagInfo
     */
    public void reward(BagInfo bagInfo)
    {
        List<DropObject> cards = getCardList();
        for(DropObject dropObject : cards)
        {
            bagInfo.addCard(null, dropObject.id, dropObject.count);
        }
        //掉落的道具
        List<DropObject> props = getPropList();
        for(DropObject dropObject : props)
        {
            bagInfo.addGoods(null, dropObject.id, dropObject.count);
        }

        //掉落的资源
        List<DropObject> assets = getAssetList();
        for(DropObject dropObject : assets)
        {
            bagInfo.addAsset(dropObject.id, dropObject.count);
        }
    }



    /***
     * 获取资源list
     * @return
     */
    public List<DropObject> getPropList(){
        //根据key在配置文件中获取所有掉落资源的配置
//        List<DropGroup> list = DataFactory.getInstance().getDataList(DataFactory.DROP_GROUP_KEY);
            //道具掉落类型如果是随机
            List<DropObject> propList = new CopyOnWriteArrayList<DropObject>();
            if( propType == E_DropType.RANDOM.getCode()){
                //根据概率计算是否掉落
                for(int i = 0 ; i < props.length; i++){
                    int r = new Random(100).nextInt();
                    if(r <= props[i].odds){
                        propList.add(props[i]);
                        break;
                    }
                }
            }
            if( propType == E_DropType.ONLY_ONE.getCode()){
                int c = 0;
                for(int i = 0 ; i < props.length; i++){
                    c += props[i].odds;
                }
                int r = new Random(c).nextInt();
                int temp = 0;
                for (int j = 0 ; j < props.length; j++){
                    temp += props[j].odds;
                    if(r <= temp){
                        propList.add(props[j]);
                        break;
                    }
                }
            }

        return propList;
    }

    /***
     * 获取掉落的道具List
     * @return
     */
    public List<DropObject> getAssetList(){
        //根据key在配置文件中获取所有掉落资源的配置
//        List<DropGroup> list = DataFactory.getInstance().getDataList(DataFactory.DROP_GROUP_KEY);
        //道具掉落类型如果是随机
        List<DropObject> assetList = new ArrayList<DropObject>();
        if( assetType == E_DropType.RANDOM.getCode()){
            //根据概率计算是否中
            for(int i = 0 ; i < assets.length; i++){
                int r = new Random(100).nextInt();
                if(r <= assets[i].odds){
                    assetList.add(assets[i]);
                    break;
                }
            }
        }
        if( assetType == E_DropType.ONLY_ONE.getCode()){
            int c = 0;
            for(int i = 0 ; i < assets.length; i++){
                c += assets[i].odds;
            }
            int r = new Random(c).nextInt();
            int temp = 0;
            for (int j = 0 ; j < assets.length; j++){
                temp += assets[j].odds;
                if(r <= temp){
                    assetList.add(assets[j]);
                    break;
                }
            }
        }

        return assetList;
    }


    /***
     * 获取资源list
     * @return
     */
    public List<DropObject> getCardList(){
        //根据key在配置文件中获取所有掉落资源的配置
//        List<DropGroup> list = DataFactory.getInstance().getDataList(DataFactory.DROP_GROUP_KEY);
        //道具掉落类型如果是随机
        List<DropObject> cardList = new ArrayList<DropObject>();
        if( cardType == E_DropType.RANDOM.getCode()){
            //根据概率计算是否中
            for(int i = 0 ; i < cards.length; i++){
                int r = new Random(100).nextInt();
                if(r <= cards[i].odds){
                    cardList.add(cards[i]);
                    break;
                }
            }
        }
        if( cardType == E_DropType.ONLY_ONE.getCode()){
            int c = 0;
            for(int i = 0 ; i < cards.length; i++){
                c += cards[i].odds;
            }
            int r = new Random(c).nextInt();
            int temp = 0;
            for (int j = 0 ; j < cards.length; j++){
                temp += cards[j].odds;
                if(r <= temp){
                    cardList.add(cards[j]);
                    break;
                }
            }
        }

        return cardList;
    }



    @Override
    public void setVariable(String key, String value)
    {
        if("props".equals(key))
        {
            String[] strs = value.split(ConstantFactory.DIVISION);
            props = new DropObject[strs.length];
            for(int i = 0; i < strs.length; i++)
            {
                props[i] = new DropObject(strs[i]);
                if(DataFactory.getInstance().getGameObject(DataFactory.GOODS_KEY, props[i].id) == null)
                {
                    System.out.println("DropGroup setVariable props error -> Goods is null:" + props[i].id);
                }
            }
        }
        else if("assets".equals(key))
        {
            String[] strs = value.split(ConstantFactory.DIVISION);
            assets = new DropObject[strs.length];
            for(int i = 0; i < strs.length; i++)
            {
                assets[i] = new DropObject(strs[i]);
                if(DataFactory.getInstance().getGameObject(DataFactory.ASSET_KEY, assets[i].id) == null)
                {
                    System.out.println("DropGroup setVariable assets error -> Asset is null:" + assets[i].id);
                }
            }
        }
        else if("cards".equals(key))
        {
            String[] strs = value.split(ConstantFactory.DIVISION);
            cards = new DropObject[strs.length];
            for(int i = 0; i < strs.length; i++)
            {
                cards[i] = new DropObject(strs[i]);
                if(DataFactory.getInstance().getGameObject(DataFactory.CARD_KEY, cards[i].id) == null)
                {
                    System.out.println("DropGroup setVariable cards error -> Card is null:" + cards[i].id);
                }
            }
        }
        else
            super.setVariable(key, value);

    }


}
